package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.Queue;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LCJZOF039 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int largestRectangleArea(int[] heights) {
            Stack<Integer> incStack = new Stack<>();
            incStack.push(-1);
            int ans = 0;

            for (int i = 0; i <= heights.length; i++) {
                int curHeight = i == heights.length ? 0 : heights[i];
                while (incStack.peek() != -1 && heights[incStack.peek()] > curHeight) {
                    int curPosMaxArea = heights[incStack.pop()] * (i - incStack.peek() - 1);
                    ans = Math.max(ans, curPosMaxArea);
                }
                incStack.push(i);
            }

            return ans;
        }
    }

    public void run() {
        System.out.println(new Solution().largestRectangleArea(InputUtil.toIntegerArray("[2,1,5,6,2,3]")));
        System.out.println(new Solution().largestRectangleArea(InputUtil.toIntegerArray("[2,4]")));
    }

    public static void main(String[] args) throws Exception {
        LCJZOF039 an = new LCJZOF039();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
